<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>canvas网格</title>
</head>
<style>
    canvas {
        border: 1px solid black;
        width: 300px;
        height: 300px;
    }
</style>

<body>
    <canvas width="500" height="500"></canvas>
</body>
<script>
    var canvas = document.getElementsByTagName("canvas")[0];
    var ctx = canvas.getContext("2d");
    ctx.lineWidth = 2;
    ctx.strokeStyle = "#000000";
    //横
    ctx.moveTo(0, 100);
    ctx.lineTo(500, 100);
    ctx.moveTo(0, 200);
    ctx.lineTo(500, 200);
    ctx.moveTo(0, 300);
    ctx.lineTo(500, 300);
    ctx.moveTo(0, 400);
    ctx.lineTo(500, 400);
    //竖
    ctx.moveTo(100, 0);
    ctx.lineTo(100, 500);
    ctx.moveTo(200, 0);
    ctx.lineTo(200, 500);
    ctx.moveTo(300, 0);
    ctx.lineTo(300, 500);
    ctx.moveTo(400, 0);
    ctx.lineTo(400, 500);
    ctx.moveTo(500, 0);
    ctx.lineTo(500, 500);
    ctx.stroke();
</script>

</html>